
# 版本说明:  2024.11.11 从实验室移动到根目录
# 制作人员:  dzy
# 功能说明： ajjl脚本的录制,调试执行,
# 备注说明：
# 环境要求： python3/ windows32or64
# 设备要求：


# ---内置库---
import os
import sys
import time
import tkinter as tk

# ---第三方库---
from colorama import init, Fore, Back, Style
import keyboard
import pyautogui
import pyperclip

# ---自研库---
from lib.mytools import getNowTime
from lib.mytools import basicClass


from macro.云效.run_v2_写云效bug_复制bug import expect_and_reality
from macro.test.run_v3_偏移删除某某 import del_item1_once, del_item1
from macro.成品入库检.run_v3_成品检验标准_新增 import fill_fqc_item, add_many, fake_scan


def tansform_test_script(actionName, actionValue, description='xxx'):
    tansform_dict = {
        '输入': f'Calvin.typewrite("{actionValue}") ',
        '点击': f'Calvin.moveAndclick({actionValue}, remark = "{description}")',
        '滚动': f'Calvin.scroll({actionValue})',
        '按键': f'Calvin.press("{actionValue}")',
    }
    code_string = f'{tansform_dict.get(actionName, "#未知")} '  # 代码
    tail_string = f'# {description} \ntime.sleep(0.3)\n'  # 代码尾巴
    code_string += tail_string  # 拼接上尾巴
    return code_string


def show_input_window(x, y):
    # 创建一个新的顶层窗口
    input_window = tk.Toplevel(root)
    input_window.title("AK弹匣")
    # 设置为置顶窗口
    input_window.attributes("-topmost", True)
    # 设置 Toplevel 窗口获取焦点
    input_window.focus_force()
    # -------------------左边的框架-------------------
    left_frame = tk.Frame(input_window, bd=2, relief=tk.SUNKEN)
    left_frame.grid(row=0, column=0)
    # 创建单选框变量
    selected_option = tk.StringVar(value="点击")
    # 创建单选框
    rb_click = tk.Radiobutton(left_frame, text="点击",
                              variable=selected_option, value="点击")
    rb_input = tk.Radiobutton(left_frame, text="输入",
                              variable=selected_option, value="输入")
    rb_scroll = tk.Radiobutton(left_frame, text="滚动",
                               variable=selected_option, value="滚动")
    rb_keypress = tk.Radiobutton(left_frame, text="按键",
                                 variable=selected_option, value="按键")

    rb_click.grid(row=0, column=0)
    rb_input.grid(row=1, column=0)
    rb_scroll.grid(row=2, column=0)
    rb_keypress.grid(row=3, column=0)

    # -------------------右边的框架----------------------
    right_frame = tk.Frame(input_window, bd=2, relief=tk.SUNKEN)
    right_frame.grid(row=0, column=1)
    # 创建输入框 点击
    click_value_label = tk.Label(right_frame, text="->:")
    click_value_entry = tk.Entry(right_frame)
    click_value_label.grid(row=0, column=0)
    click_value_entry.grid(row=0, column=1)
    # 将鼠标坐标写入输入框
    click_value_entry.insert(0, f"{x}, {y}")

    # 创建输入框2 - 输入
    input_value_label = tk.Label(right_frame, text="->:")
    input_value_entry = tk.Entry(right_frame)
    input_value_label.grid(row=2, column=0)
    input_value_entry.grid(row=2, column=1)
    input_value_entry.insert(0, '输入一些文本')

    # 创建输入框3  - 滚动
    scroll_value_label = tk.Label(right_frame, text="->:")
    scroll_value_entry = tk.Entry(right_frame)
    scroll_value_label.grid(row=3, column=0)
    scroll_value_entry.grid(row=3, column=1)
    scroll_value_entry.insert(0, '-400')

    # 创建输入框4 - 按键
    keypress_value_label = tk.Label(right_frame, text="->:")
    keypress_value_entry = tk.Entry(right_frame)
    keypress_value_label.grid(row=4, column=0)
    keypress_value_entry.grid(row=4, column=1)
    keypress_value_entry.insert(0, 'enter')

    # 写入按钮的回调函数

    def write_to_file():
        with open("./data/selected_option.txt", "a", encoding='utf-8') as f:
            actionName = selected_option.get()
            value_dict1 = {
                "点击": click_value_entry.get(),
                "输入": input_value_entry.get(),
                "滚动": scroll_value_entry.get(),
                "按键": keypress_value_entry.get(),
            }
            actionValue = value_dict1.get(selected_option.get(), '未获取到动作值')

            description = remark_entry.get()

            str1 = selected_option.get() + f"坐标: ({x}, {y}) \n"
            str1 = tansform_test_script(actionName, actionValue, description)
            f.write(str1)
        print(f"选中的选项: {selected_option.get()} {description} 已写入文件")
        input_window.destroy()

    # -------------------下方的框架----------------------
    remark_label = tk.Label(input_window, text="备注:")
    remark_entry = tk.Entry(input_window)

    remark_label.grid(row=1, column=0)
    remark_entry.grid(row=1, column=1)
    # 设置焦点到输入框
    remark_entry.focus_set()

    # 创建写入按钮
    write_button = tk.Button(input_window, text="写入", command=write_to_file)
    write_button.grid()
    # 绑定回车键到写入按钮
    input_window.bind('<Return>', lambda event: write_button.invoke())
    input_window.bind('<Escape>', lambda event: input_window.destroy())

    # 将窗口位置设置为鼠标位置
    input_window.geometry(f"+{x}+{y}")

    remark_entry.insert(0, pyperclip.paste())


def hotkey_action():
    x, y = pyautogui.position()  # 获取鼠标当前位置
    show_input_window(x, y)


# 创建主窗口
root = tk.Tk()
root.withdraw()  # 隐藏主窗口

key1_alt_and_wave = 'alt+`'
key2_f2 = 'f2'
key3_f8 = 'f8'
# endKey = 'f10'

# 绑定全局热键
keyboard.add_hotkey(key1_alt_and_wave, hotkey_action)
keyboard.add_hotkey(key2_f2, expect_and_reality)
keyboard.add_hotkey(key3_f8, fake_scan)

msg = f'{getNowTime()} 启动完成'
msg += f'注册的快捷键 {key1_alt_and_wave, key2_f2, key3_f8}'
print(msg)
# keyboard.wait(endKey)
# 启动tkinter的主循环
root.mainloop()
